import request from '@/utils/request'

/**
 * 获取首页-文章列表
 * @param {*} channel_id 频道id
 * @returns Promise对象
 */
// 解构赋值失败情况: 字段的值为undefined的时候, 才会用=后面的默认值
export const getArticleListAPI = ({ channel_id, timestamp = new Date() }) =>
  request({
    url: '/v1_0/articles',
    method: 'GET',
    params: {
      channel_id,
      timestamp
    }
    // axios发现params的参数名对应的值为null, 会自动忽略此参数不拼接在url?后面
  })

// 首页-反馈-不喜欢文章
export const dislikeArticleAPI = (artId) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  data: {
    target: artId
  }
})

// 首页-反馈-举报文章
export const reportArticleAPI = ({ artId, type }) => request({
  url: '/v1_0/article/reports',
  method: 'POST',
  data: {
    target: artId,
    type: type,
    remark: '就是其他问题' // 思路: 点击其他问题, 显示一个输入框, 点击确定的时候, 再调用接口
  }
})
// 获取文章详情
export const getArticleDetailAPI = ({ artId }) => request({
  url: `/v1_0/articles/${artId}`
})
// 关注作者
export const followedAuthorAPI = (authorId) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: authorId
  }
})
// 取关作者
export const unFollowedAuthorAPI = (authorId) => request({
  url: `/v1_0/user/followings/${authorId}`,
  method: 'DELETE'
})
// 点赞
export const likeAuthorAPI = (artId) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: artId
  }
})
// 取消点赞
export const unLikeAuthorAPI = (artId) => request({
  url: `/v1_0/article/likings/${artId}`,
  method: 'DELETE'
})
// 获取评论
export const getCommentListAPI = ({ artId, offset = null, limit = 10 }) => request({
  url: '/v1_0/comments',
  params: {
    type: 'a',
    source: artId,
    offset: offset, // axios遇到值为空时i，回忽略当前的参数key的值null
    limit: limit
  }
})
// 评论点赞
export const likeCommentAPI = (commentId) => request({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target: commentId
  }
})
// 评论取消点赞
export const unLikeCommentAPI = (commentId) => request({
  url: `/v1_0/comment/likings/${commentId}`,
  method: 'DELETE'
})
// 发布评论
export const sendCommentAPI = ({ target, content, art_id = null }) => {
  const obj = {
    target: target,
    content: content
  }
  if (art_id !== null) {
    obj.art_id = art_id
  }
  return request({
    url: 'v1_0/comments',
    method: 'POST',
    data: obj
  })
}
